import { getSignService } from '@/api/login'
import * as ww from '@wecom/jssdk'

export async function getSign() {
  const url = location.href.split('#')[0]
  const res = await getSignService({ url })
  console.log('jssdk', res)
  const { corpid, timestamp, nonceStr, configSignature, agentConfigSignature } = res
  return new Promise((resolve,reject)=>{
    ww.register({
      corpId: corpid, // 必填，公众号的唯一标识
      agentId: 1000034,
      jsApiList: ['getLocation', 'chooseImage', 'onHistoryBack', 'scanQRCode'], // 必填，需要使用的JS接口列表
      getConfigSignature() {
        return {
          timestamp,
          nonceStr,
          signature: configSignature
        }
      }, // 必填，根据url生成企业签名的回调函数
      // getAgentConfigSignature () {
      //   return { timestamp, nonceStr, signature: agentConfigSignature }
      // },
      onConfigSuccess: (res) => {
        const msg = 'onConfigSuccess success1' + JSON.stringify(res)
        // alert(msg)
        resolve()
      },
      onConfigFail: (res) => {
        const msg = 'onConfigFail  failed1' + JSON.stringify(res)
        // alert(msg)
      },
      // onAgentConfigSuccess: () => {
      //   alert('onAgentConfigSuccess success')
      // },
      // onAgentConfigFail: () => {
      //   alert('onAgentConfigSuccess failed')
      // },
      onConfigComplete: (res) => {
        const msg = 'onConfigComplete1' + JSON.stringify(res)
        // alert(msg)
      }
      // onAgentConfigComplete: (res) => {
      //   console.log('onAgentConfigComplete', JSON.stringify(res))
      // }
    })
    // console.log(ww.register)
  })
}
